// 按过滤条件获取 lnks 窗口
// 此处不必要使用 Promise ，因为窗口创建成功与否并不能保证。
window.getLnksWin = (filterObj, update=false) => {
    if (window.lnks) {
        if (window.lnksWins) {
            for (const lnksWin of lnksWins) {
                console.log(`遍历窗口: title:${lnksWin.title}, appId:${lnksWin.appId}, url:${lnksWin.url}`)
                if (lnksWin.appId == filterObj.appId || lnksWin.url == filterObj.url || lnksWin.title == filterObj.title) {
                    return lnksWin
                }
            }
        }
        if (update) {
            window.lnksWins = lnks.MainWindow.all();
            return window.getLnksWin(filterObj, false);
        }
    }
    return null;
}
// 获取初始化好的 lnks 托盘坐标
window.getTrayGeometryPromise = (trayIcon) => {
    return new Promise(function (resolve, reject) {
        let wait = () => {
            if (trayIcon.geometry.width === 0) {
                console.log("getTrayGeometry Func:", trayIcon.geometry)
                // WARNING: AMD64 KDE can`t get trayIcon.geometry
                setTimeout(wait, 600);
            } else {
                resolve(win);
            }
        };
        wait();
    });
}

window.friendlyNotice = (obj) => {
    $('.notice')
        .removeClass("notice_error")
        .addClass("notice_success")
        .text(JSON.stringify(obj) + (new Date()).toLocaleString('zh-CN'))
        .show();
}

window.getRandomInt = (max) => {
    return Math.floor(Math.random() * max);
}

// -----------------------------------------------------
// 窗口测试公共函数
// -----------------------------------------------------
// 窗口移动测试函数
window.WinMoveRandomPositionFunc = async (win, waitFunc, getTestStateFunc) => {
    win.setPosition(
        getRandomInt(screen.availWidth),
        getRandomInt(screen.availHeight)
    ); // 移动窗口会自动聚焦
    win.setFocus();
    await waitFunc(1000);
    //await getTestStateFunc(JSON.stringify(p), win.position, `设置了位置：${JSON.stringify(p)}`);
}